package etnorservice.etnor_service.controller;

import etnorservice.etnor_service.domain.po.ApiDTO;
import etnorservice.etnor_service.domain.po.ApiStatisticsDTO;
import etnorservice.etnor_service.domain.po.MenuItem;
import etnorservice.etnor_service.domain.po.Products;
import etnorservice.etnor_service.result.Result;
import etnorservice.etnor_service.service.IApiService;
import etnorservice.etnor_service.service.MenuService;
import etnorservice.etnor_service.service.UserService;
import etnorservice.etnor_service.utils.JwtTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: cc
 * @Date: 2024/10/11/12:30
 **/
@RestController
public class MainPageController {
    @Autowired
    MenuService menuService;
    @Autowired
    UserService userService;
    @Autowired
    IApiService apiService;
    @GetMapping("/preData/menu")
    private Result getPreInfoProducts(@RequestHeader(value = "Authorization", required = false)
                                                  String authorizationHeader){
        String account =
                new JwtTokenUtil().
                        getAccountFromToken(extractTokenFromHeader(authorizationHeader));
        Integer id = Integer.parseInt(account);
        Integer talentId = userService.findRoleByAccount(id).getId();
        Result result=new Result();
        //获取用户角色
        System.out.println("============================>用户id"+talentId);

        //获取用户权限
        List<MenuItem> menuItems = userService.findUserPermissions(talentId);
        System.out.println("==============================>权限集合"+menuItems);
//        result.setData(menuService.buildMenuTree());
        result.setData(convertMenuItems(menuItems));
        return result;
    }
    private String extractTokenFromHeader(String header) {
        if (header == null || !header.startsWith("Bearer ")) {
            return null;
        }
        return header.substring(7).trim(); // 去除 "Bearer ".length() 后面的空格
    }


    public static List<MenuItem> convertMenuItems(List<MenuItem> menuItems) {
        // 创建一个用于存储最终结果的 List
        List<MenuItem> result = new ArrayList<>();

        // 创建一个 Map 用于存储父子关系
        Map<Integer, MenuItem> menuItemMap = new HashMap<>();

        // 遍历菜单项，构建 Map
        for (MenuItem item : menuItems) {
            menuItemMap.put(item.getId(), item);
            item.setChildren(new ArrayList<>()); // 初始化子项列表
        }

        // 构建父子关系
        for (MenuItem item : menuItems) {
            if (item.getParentId() == 0) {
                // 如果是父项，添加到结果列表
                result.add(item);
            } else {
                // 如果是子项，添加到其父项的子项列表
                MenuItem parent = menuItemMap.get(item.getParentId());
                if (parent != null) {
                    parent.getChildren().add(item);
                }
            }
        }

        return result;
    }
    //获取控制面板统计数据 getApiStatistics
    @GetMapping("/admin/preData/getApiStatistics")
    private Result getPreInfoProducts(){
        List<ApiStatisticsDTO> apiStatistics = apiService.getApiStatistics();
        Integer sumDeveloper = userService.getSumDeveloper();
        apiStatistics.get(0).setDevelopers(sumDeveloper);
        List<ApiDTO> allApis = apiService.getAllApis();
        apiStatistics.get(0).setApis(allApis.size());

        Result result = new Result();
        result.setCode(200);
        result.setData(apiStatistics);
        return result;
    }



}
